<template>
  <BasicModal v-bind="$attrs" destroyOnClose title="审核" :width="modalWidth" :okButtonProps="{ disabled: false }" @register="registerModal" :destroyOnClose="true" @ok="handleSubmit">
    <Description size="middle" class="mt-4" :bordered="false" :column="{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }" :data="vipRecordInfo.data" :schema="vipRecordInfoSchema" />
    <BasicForm @register="registerForm"></BasicForm>
  </BasicModal>
</template>
<script lang="ts" setup>
import { BasicModal, useModalInner } from '@apps/antd-admin/src/components/modal';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/form';
import { checkFormSchema } from '../data/viprecord.data';
import { reactive, ref, unref } from 'vue';
import { getBusViprecord, check } from '../data/viprecord.api';
import { DescItem, Description } from '@apps/antd-admin/src/components/description';
import { useAppInject } from '@apps/antd-admin/src/hooks/web/use-app-inject';
const { getIsMobile } = useAppInject();
const modalWidth = ref('550px');
if (unref(getIsMobile)) {
  modalWidth.value = '80%';
}
let vipRecordInfo: Recordable = reactive({ data: {} });
const vipRecordInfoSchema: DescItem[] = [
  {
    field: 'goodsName',
    label: '卡种名称',
    span: 3,
  },
  {
    field: 'remark',
    label: '销售备注',
    span: 10,
  },
];
const emit = defineEmits(['success', 'register']);
const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
  labelWidth: 90,
  schemas: checkFormSchema,
  showActionButtonGroup: false,
});
const isUpdate = ref(true);
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  resetFields();
  data.value = !!data?.isUpdate;

  if (unref(isUpdate)) {
    let record = data.record;
    let queryData = await getBusViprecord({ vipRecordId: record.vipRecordId });
    vipRecordInfo.data = queryData;
    let newData = { vipRecordId: queryData.vipRecordId };
    setFieldsValue({
      ...newData,
    });
  }
});

async function handleSubmit() {
  try {
    const values = await validate();
    console.info(values);
    setModalProps({ confirmLoading: true });
    if (unref(isUpdate)) {
      await check(values);
    }
    closeModal();
    emit('success', false);
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>
